import { useTable, LIST_TYPE } from '@crud/index';
import dayjs from 'dayjs';
import { getList } from '@/api/crm-cy/user';
import { toolOptions2Map } from '@/utils';

// 会员类型
export const vipLevelOptions = [
  { label: '不限', value: '' },
  { label: '钻石会员', value: 20 },
  { label: '黄金会员', value: 10 },
  { label: '白银会员', value: 5 },
  { label: '体验会员', value: 0 }
];
/** 快速日期枚举 */
export const quickDateMap = [
  {
    label: '3天',
    value: [3, 'd']
  },
  {
    label: '5天',
    value: [5, 'd']
  },
  {
    label: '7天',
    value: [7, 'd']
  },
  {
    label: '14天',
    value: [14, 'd']
  },
  {
    label: '15天',
    value: [15, 'd']
  },
  {
    label: '一月',
    value: [1, 'month']
  },
  {
    label: '一季',
    value: [3, 'month']
  },
  {
    label: '半年',
    value: [6, 'month']
  },
  {
    label: '一年',
    value: [1, 'year']
  }
];

export const vipLevelMap = toolOptions2Map(vipLevelOptions);

// 表格
export const useTableList = (options: any = {}) => {
  const columns = [
    { label: '用户ID', prop: 'id', minWidth: 120, align: 'letf', fixed: 'left' },
    { label: '用户昵称', prop: 'nick_name', width: 228, showOverflowTooltip: true, align: 'letf' },
    { label: '手机号', prop: 'mobile', minWidth: 120, align: 'center' },
    { label: '注册时间', prop: 'add_time', width: 180, type: 'dateTime', align: 'center' },
    { label: '会员类型', prop: 'level', width: 120, render: (s: any) => vipLevelMap[s.row.level] || '无会员', align: 'center' },
    { label: '到期时间', prop: 'end_time', width: 180, type: 'dateTime', align: 'center' },
    { label: '最后登录时间', prop: 'last_login_time', width: 180, type: 'dateTime', align: 'center' },
    { label: '一级渠道', prop: 'utm_source', width: 120, align: 'center', render: (s: any) => s.row.utm_source || '--' },
    { label: '二级渠道', prop: 'utm_term', width: 120, align: 'center', render: (s: any) => s.row.utm_term || '--' },
    { label: '封号', prop: 'expire', width: 120, align: 'center', render: (s: any) => s.row.is_frozen ? '是' : '否' },
    { label: '备注', prop: 'remark', width: 150, align: 'center', render: (s: any) => s.row.remark || '--' },
    { label: '操作', align: 'center', render: 'actions', width: 120, fixed: 'right' }
  ];

  return useTable({
    ...options,
    type: LIST_TYPE.CY,
    params: {},
    columns,
    getParams: ({ _dateRange, ...params }) => {
      if (_dateRange?.length > 0) {
        const [start, end] = _dateRange ?? [];
        /** 清空用户标签字段 */
        params = {
          ...params,
          from_dt: undefined,
          to_dt: undefined
        };
        params.from_dt = dayjs(start).startOf('d').format('X');
        params.to_dt = dayjs(end).endOf('d').format('X');
      }
      return params;
    },
    getList
  });
};
